<template>
  <div class="list-table">
    <div class="area-header">
      <span class="area-header-title">{{ $t('基金项目') }}</span>
    </div>
    <el-form ref="queryForm" :model="queryParams" :inline="true" @submit.prevent.native>
      <el-form-item :label="$t('基金名称')" prop="fundName">
        <el-input
          v-model="queryParams.fundName"
          :placeholder="$t('基金名称')"
          clearable
          @keyup.enter.native="queryHandle"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="queryHandle">{{ $t('搜索') }}</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">{{ $t('重置') }}</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          v-hasPermi="['bf:investGroup:edit']"
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="addHandle"
        >{{ $t('新增') }}
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          v-hasPermi="['bf:investGroup:import']"
          type="info"
          icon="el-icon-upload2"
          size="mini"
          @click="handleImport"
        >{{ $t('导入') }}
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          v-hasPermi="['bf:investGroup:export']"
          type="warning"
          icon="el-icon-download"
          size="mini"
          @click="exportHandle"
        >{{ $t('导出') }}
        </el-button>
      </el-col>
    </el-row>

    <el-table v-loading="loading" :data="bfInvestFundList" border tooltip-effect="darkTable">
      <el-table-column :label="$t('序号')" type="index" fixed="left" align="center" show-overflow-tooltip width="50">
        <template slot-scope="scope">
          {{ (queryParams.pageNum - 1) * queryParams.pageSize + scope.$index + 1 }}
        </template>
      </el-table-column>
      <el-table-column v-if="this.$route.query.source === 'BF_PORTFOLIO'" :label="$t('基金项目名称')" align="center" prop="fundName" fixed="left" min-width="180" show-overflow-tooltip>
        <template slot-scope="scope">
          <el-link type="primary" @click="updateHandle(scope.row)">{{ scope.row.fundName }}</el-link>
        </template>
      </el-table-column>
      <el-table-column v-if="this.$route.query.source === 'BF_PORTFOLIO'" :label="$t('母基金名称')" align="center" prop="dealName" min-width="180" show-overflow-tooltip>
        <template slot-scope="scope">
          <router-link
            target="_blank"
            :to="{path:'/deal/home/dealBaseHome',query:{id:scope.row.dealId}}"
            class="link_a"
          >{{ scope.row.dealName }}</router-link>
        </template>
      </el-table-column>
      <el-table-column v-if="this.$route.query.source !== 'BF_PORTFOLIO'" :label="$t('基金名称')" align="center" prop="fundName" fixed="left" min-width="180" show-overflow-tooltip>
        <template slot-scope="scope">
          <el-link type="primary" @click="updateHandle(scope.row)">{{ scope.row.fundName }}</el-link>
        </template>
      </el-table-column>
      <el-table-column :label="$t('基金管理人名称')" align="center" prop="managerName" min-width="180" show-overflow-tooltip />
      <el-table-column :label="$t('基金注册成立日')" align="center" prop="fundSetTime" min-width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.fundSetTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="$t('基金期限')" align="center" prop="fundTerm" min-width="180" show-overflow-tooltip />
      <el-table-column :label="$t('基金当前阶段')" align="center" prop="fundPhase" :formatter="fundPhaseFormat" min-width="180" show-overflow-tooltip />
      <el-table-column :label="$t('首次出资时间')" align="center" prop="firstInvestmentTime" min-width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.firstInvestmentTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="$t('基金总规模')+$t('金额单位')" align="center" prop="fundSize" min-width="180" show-overflow-tooltip>
        <template slot-scope="scope">
          <span class="money-text">{{ moneyFormat(scope.row.fundSize, 'w') }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="$t('母基金认缴金额')+$t('金额单位')" align="center" prop="subscriptionAmount" min-width="180" show-overflow-tooltip>
        <template slot-scope="scope">
          <span class="money-text">{{ moneyFormat(scope.row.subscriptionAmount, 'w') }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="$t('母基金认缴比例(%)')" align="center" prop="subscriptionRatio" min-width="180" show-overflow-tooltip>
        <template slot-scope="scope">
          <span>{{ moneyFormat(scope.row.subscriptionRatio) }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="$t('母基金实缴金额')+$t('金额单位')" align="center" prop="paidAmount" min-width="180" show-overflow-tooltip>
        <template slot-scope="scope">
          <span class="money-text">{{ moneyFormat(scope.row.paidAmount, 'w') }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="$t('母基金出资进度(%)')" align="center" prop="contributionProgress" min-width="180" show-overflow-tooltip>
        <template slot-scope="scope">
          <span>{{ moneyFormat(scope.row.contributionProgress) }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="$t('母基金回收金额')+$t('金额单位')" align="center" prop="recoveryAmount" min-width="180" show-overflow-tooltip>
        <template slot-scope="scope">
          <span class="money-text">{{ moneyFormat(scope.row.recoveryAmount, 'w') }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="$t('母基金回收进度(%)')" align="center" prop="recoveryProgress" min-width="180" show-overflow-tooltip>
        <template slot-scope="scope">
          <span>{{ moneyFormat(scope.row.recoveryProgress) }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="$t('母基金持有最新估值')+$t('金额单位')" align="center" prop="valuation" min-width="180" show-overflow-tooltip>
        <template slot-scope="scope">
          <span class="money-text">{{ moneyFormat(scope.row.valuation, 'w') }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="$t('回报倍数')" align="center" prop="returnMultiple" min-width="180" show-overflow-tooltip>
        <template slot-scope="scope">
          <span>{{ moneyFormat(scope.row.returnMultiple) }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="$t('项目状态')" align="center" prop="status" :formatter="statusFormat" min-width="180" show-overflow-tooltip />
      <el-table-column
        :label="$t('操作')"
        fixed="right"
        align="center"
        class-name="small-padding fixed-width"
        width="200"
      >
        <template slot-scope="scope">
          <el-button
            v-hasPermi="['bf:investGroup:edit']"
            size="small"
            type="text"
            icon="el-icon-edit"
            @click="updateHandle(scope.row)"
          >{{ $t('修改') }}
          </el-button>
          <el-button
            v-hasPermi="['bf:investGroup:remove']"
            size="small"
            type="text"
            icon="el-icon-delete"
            @click="deleteHandle(scope.row)"
          >{{ $t('删除') }}
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改基金项目对话框 -->
    <el-dialog
      :title="$t(title)"
      :visible.sync="formOpen"
      :close-on-click-modal="false"
      :lock-scroll="false"
      custom-class="dialogForm"
      append-to-body
    >
      <bfInvestFundForm ref="bfInvestFundForm" />
    </el-dialog>

    <!-- 导入 -->
    <el-dialog
      :title="$t('基金项目导入')"
      :visible.sync="upload.open"
      width="400px"
      :close-on-click-modal="false"
      :lock-scroll="false"
    >
      <el-upload
        ref="upload"
        :limit="1"
        accept=".xlsx, .xls"
        :headers="upload.headers"
        :action="upload.url + '?updateSupport=' + upload.updateSupport+ '&dealId=' + upload.dealId"
        :disabled="upload.isUploading"
        :on-progress="handleFileUploadProgress"
        :on-success="handleFileSuccess"
        :auto-upload="false"
        drag
      >
        <i class="el-icon-upload" />
        <div class="el-upload__text">
          {{ $t('将文件拖到此处，或') }}
          <em>{{ $t('上传') }}</em>
        </div>
        <!--        <div slot="tip" class="el-upload__tip">-->
        <!--          <el-checkbox v-model="upload.updateSupport" /> {{ $t('是否更新已经存在的数据') }}-->
        <!--        </div>-->
        <div slot="tip" class="el-upload__tip" style="color:red">
          {{ $t('提示：仅允许导入“xls”或“xlsx”格式文件！') }}
          <el-link type="primary" style="font-size:12px" @click="fetchTemplate">
            {{ $t('下载模板') }}
          </el-link>
        </div>
      </el-upload>
      <div slot="footer" class="dialog-footer">
        <el-button @click="upload.open = false">{{ $t("取消") }}</el-button>
        <el-button type="primary" @click="submitFileForm">{{ $t("确定") }}</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listBfInvestFund, delBfInvestFund } from '@/api/bf/investGroup/bfInvestFund'
import bfInvestFundForm from './bfInvestFundForm.vue'
import { getToken } from '@/utils/auth'

export default {
  components: {
    bfInvestFundForm
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 总条数
      total: 0,
      // 基金项目表格数据
      bfInvestFundList: [],
      // 基金当前阶段字典
      fundPhaseOptions: [],
      // 项目状态字典
      statusOptions: [],
      // 弹出层标题
      title: '基金项目',
      // 是否显示弹出层
      formOpen: false,
      // 查询参数
      queryParams: {
        dealId: this.$route.query.id,
        fundName: undefined,
        pageNum: 1,
        pageSize: 10
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
      // 用户导入参数
      upload: {
        // 是否显示弹出层（用户导入）
        open: false,
        // 弹出层标题（用户导入）
        title: '',
        // 是否禁用上传
        isUploading: false,
        // 是否更新已经存在的用户数据
        updateSupport: 0,
        // dealId
        dealId: this.$route.query.id,
        // 设置上传的请求头部
        headers: { Authorization: 'Bearer ' + getToken() },
        // 上传的地址
        url: process.env.VUE_APP_BASE_API + '/investGroup/bfInvestFund/importData'
      }
    }
  },
  created() {
    this.getList()
    this.getDicts('fund_phase').then(response => {
      this.fundPhaseOptions = response.data
    })
    this.getDicts('quit_status').then(response => {
      this.statusOptions = response.data
    })
  },
  methods: {
    /** 查询基金项目列表 */
    getList() {
      this.loading = true
      if (this.$route.query.source !== 'BF_PORTFOLIO' && this.$parent && this.$parent.$parent !== undefined && this.$parent.$parent.getInvestmentSummary !== undefined) {
        // 刷新投资汇总
        this.$parent.$parent.getInvestmentSummary()
      }
      listBfInvestFund(this.queryParams).then(response => {
        this.bfInvestFundList = response.rows
        this.total = response.total
        this.loading = false
      }).catch(() => {
        this.loading = false
      })
    },
    // 项目状态字典翻译
    statusFormat(row) {
      return this.selectDictLabel(this.statusOptions, row.status)
    },
    fundPhaseFormat(row) {
      return this.selectDictLabel(this.fundPhaseOptions, row.fundPhase)
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        dealId: undefined,
        fundName: undefined,
        managerName: undefined,
        firstInvestmentTime: undefined,
        fundSize: undefined,
        subscriptionAmount: undefined,
        subscriptionRatio: undefined,
        paidAmount: undefined,
        contributionProgress: undefined,
        recoveryAmount: undefined,
        recoveryProgress: undefined,
        valuation: undefined,
        returnMultiple: undefined,
        status: '0',
        createBy: undefined,
        createTime: undefined,
        updateBy: undefined,
        updateTime: undefined,
        delFlag: undefined
      }
      this.resetForm('form')
    },
    /** 搜索按钮操作 */
    queryHandle() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.queryHandle()
    },
    /** 新增按钮操作 */
    addHandle() {
      this.formOpen = true
      this.$nextTick(() => {
        this.$refs.bfInvestFundForm.init(null)
      })
    },
    /** 修改按钮操作 */
    updateHandle(row) {
      this.formOpen = true
      this.$nextTick(() => {
        // 调用form页面的 init 方法初始化数据
        this.$refs.bfInvestFundForm.init(row)
      })
    },
    /** 删除按钮操作 */
    deleteHandle(row) {
      this.$confirm(this.$t('是否确认删除?'), this.$t('警告'), {
        confirmButtonText: this.$t('确定'),
        cancelButtonText: this.$t('取消'),
        type: 'warning'
      }).then(() => {
        return delBfInvestFund(row.id)
      }).then(() => {
        this.getList()
        this.msgSuccess(this.$t('删除成功'))
      }).catch(() => {
        this.loading = false
      })
    },
    /** 导出按钮操作 */
    exportHandle() {
      this.$confirm(this.$t('是否确认导出数据项?'), this.$t('警告'), {
        confirmButtonText: this.$t('确定'),
        cancelButtonText: this.$t('取消'),
        type: 'warning'
      }).then(() => {
        this.downLoadExcel('/investGroup/bfInvestFund/export', this.queryParams)
      }).then(response => {
        this.download(response.msg)
      }).catch(() => {
      })
    },
    /** 导入按钮操作 */
    handleImport() {
      this.upload.title = this.$t('导入')
      this.upload.open = true
    },
    // 文件上传中处理
    handleFileUploadProgress() {
      this.upload.isUploading = true
    },
    // 文件上传成功处理
    handleFileSuccess(response) {
      this.upload.open = false
      this.upload.isUploading = false
      this.$refs.upload.clearFiles()
      this.$alert(response.msg, this.$t('导入结果'), { dangerouslyUseHTMLString: true })
      this.getList()
    },
    // 下载模板
    fetchTemplate() {
      const href1 = window.location.href
      const href2 = this.$route.path
      const url = href1.substring(0, href1.indexOf(href2))
      window.location.href = url + '/template/bfFundProjectImportTemplate.xlsx'
    },
    // 提交上传文件
    submitFileForm() {
      this.$refs.upload.submit()
    }
  }
}
</script>
